package com.qc31.monitorlib.ui.line

import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import autodispose2.autoDispose
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ktx.immersionBar
import com.qc31.baselib.base.BaseActivity
import com.qc31.baselib.rx.clickThrottle
import com.qc31.baselib.utils.BuildConfigs
import com.qc31.comlib.Keys
import com.qc31.comlib.adapter.TabPagerAdapter
import com.qc31.comlib.databinding.IncludeTabLayoutBinding
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivityHistoryInfoLineBinding
import com.qc31.monitorlib.uitls.DataUtil
import com.qc31.monitorlib.uitls.WxShareModel

class HistoryLineInfoActivity :
    BaseActivity<ActivityHistoryInfoLineBinding>(ActivityHistoryInfoLineBinding::inflate) {
    private lateinit var mapFragment: MapLineFragment
    private lateinit var mTabVB: IncludeTabLayoutBinding
    private val mViewModel by viewModels<LineInfoViewModel>()
    private lateinit var carName: String
    override fun initView() {
        mTabVB = IncludeTabLayoutBinding.bind(binding.root)
        carName = intent?.getStringExtra(Keys.INTENT_TITLE) ?: ""
        val speed = intent?.getStringExtra(Keys.INTENT_OTHER) ?: "0"

        immersionBar { titleBar(binding.rlToolbar) }
        binding.tvMainTitle.text = "${getString(R.string.title_history_line_info)}-$carName"
        if (!BuildConfigs.ISCOMPANY) binding.ivShareLine.visibility = View.GONE

        mapFragment = MapLineFragment()
        mTabVB.tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        mViewModel.speed.value = speed.toFloat()
        mViewModel.data.value = DataUtil.HistoryLine
        initFragments()
    }

    override fun setListener() {
        binding.ivShareLine.isVisible = !BuildConfigs.ISGOOGLE && BuildConfigs.ISCOMPANY
        val params = intent.getStringExtra(Keys.INTENT_DATA)!!
        val split = params.split(",")
        val carId = split[0]
        val stime = split[1]
        val etime = split[2]
        val filter0 = split[3]
        val filterInvalid = split[4]
        val filterAppend = split[5]
        val s_time = split[6]
        val e_time = split[7]
//        binding.ivShareLine.clickThrottle().autoDispose(scopeProvider).subscribe {
//            WxShareUtil.instance.init(applicationContext)
//            wxViewModel.shareLine(
//                carName,
//                carId,
//                stime,
//                etime,
//                filter0,
//                filterInvalid,
//                filterAppend,
//                s_time, e_time
//            )
//        }

        binding.ivToolbarBack.setOnClickListener {
            finish()
        }
    }

    private val wxViewModel by viewModels<WxShareModel>()

    private fun tabTitles(): ArrayList<String> {
        val titles = ArrayList<String>()
        titles.add(getString(R.string.history_line_tab_whole))
        titles.add(getString(R.string.history_line_tab_map))

        titles.add(
            String.format(
                "%s(%d)",
                getString(R.string.history_line_tab_alarm),
                DataUtil.HistoryLine?.alarmData?.size ?: 0
            )
        )
        titles.add(
            String.format(
                "%s(%d)",
                getString(R.string.history_line_tab_stop),
                DataUtil.HistoryLine?.stopData?.size ?: 0
            )
        )
        titles.add(
            String.format(
                "%s(%d)",
                getString(R.string.history_line_tab_over_speed),
                DataUtil.HistoryLine?.overspeedData?.size ?: 0
            )
        )

        for (num in titles) {
            mTabVB.tabLayout.addTab(mTabVB.tabLayout.newTab())
        }

        /** tabLayout 点击事件监听 */
        mTabVB.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab?.position ?: 0
                binding.mViewPager.currentItem = position
                if (position != 0) {
                    mapFragment.stopMove()
                }
            }
        })
        return titles
    }

    private fun initFragments() {
        val fragments = ArrayList<Fragment>()
        fragments.add(WholeFragment())
        fragments.add(mapFragment)
        fragments.add(AlarmLineFragment())
        fragments.add(StopLineFragment())
        fragments.add(OverSpeedFragment())
        binding.mViewPager.adapter = TabPagerAdapter(supportFragmentManager, fragments, tabTitles())
        binding.mViewPager.currentItem = 0
        binding.mViewPager.offscreenPageLimit = 5
        mTabVB.tabLayout.setupWithViewPager(binding.mViewPager)
    }

    override fun onDestroy() {
        super.onDestroy()
        DataUtil.HistoryLine = null
    }
}
